/****************************************************************
 * <strong>MFSQLSelect基类</strong>
 *
 * <p>
 * MFSQLSelect基类
 * </p>
 *
 * @author AndyZhang 2011 cnet2001@163.com
 * @version 0.1
 * ToDo:
 ***************************************************************/
package com.thinkwide.data.ormapping.sql;

import com.thinkwide.data.core.MFString;

public class MFSqlSelect extends MFSqlBase {

    public final static String SELECT_SIMPLE = "Select * From %s ";
    public final static String SELECT_PAGE = "Select * From %s ";
    public final static String SELECT_COLUMN = "Select %s From %s ";
    public final static String SELECT_COLUMN_ORDER = "Select %s From %s %s ";
    public final static String SELECT_COUNT = "Select count(*) From %s %s ";
    public final static String SELECT_COUNT_FROM = "Select count(*) From (%s) ";

    private static String getSelectPageSql(String tableName, String where, String orderBy, int beginIndex, int endIndex) {
        String sql = "SELECT * FROM (SELECT A.*,ROWNUM RN FROM (SELECT * FROM %s %s %s ) A) Where RN>%s AND RN<=%s ";

        String str = String.format(sql, tableName, where, orderBy, beginIndex, endIndex);
        return str;
    }

    private static String getSelectPageSqlExt(String sql, String where, String orderBy, int beginIndex, int endIndex) {
        String xwhere = "";
        if (MFString.notNullOrEmpty(where)) {
            xwhere = " where " + where;
        }

        String xorderBy = "";
        if (MFString.notNullOrEmpty(orderBy)) {
            xorderBy = " order by " + orderBy;
        }

        String sqlformat = "SELECT * FROM (SELECT A.*,ROWNUM RN FROM (%s ) A %s %s) Where RN>%s AND RN<=%s ";

        String str = String.format(sqlformat, sql, where, xorderBy, beginIndex, endIndex);
        return str;
    }

    private static String getSelectPageSql(String sql, int beginIndex, int endIndex) {
        String sqlformat = "SELECT * FROM (SELECT A.*,ROWNUM RN FROM (%s ) A) Where RN>%s AND RN<=%s ";

        String str = String.format(sqlformat, sql, beginIndex, endIndex);
        return str;
    }

    public static String getWhereConditionSql(String whereFirst, String whereSecond) {

        if (MFString.isNullOrEmpty(whereFirst)) {
            if (MFString.isNullOrEmpty(whereSecond)) {
                return MFString.Empty;
            } else {
                return WHERE + whereSecond;
            }

        } else {
            if (MFString.isNullOrEmpty(whereSecond)) {
                return WHERE + whereFirst;
            } else {
                return WHERE + whereFirst + AND + whereSecond;
            }
        }

    }

    public static String getOrderByConditionSql(String orderByFirst, String orderBySecond) {
        if (MFString.isNullOrEmpty(orderByFirst)) {
            if (MFString.isNullOrEmpty(orderBySecond)) {
                return MFString.Empty;
            } else {
                return ORDER_BY + orderBySecond;
            }

        } else {
            if (MFString.isNullOrEmpty(orderBySecond)) {
                return ORDER_BY + orderByFirst;
            } else {
                return ORDER_BY + orderByFirst + COMMA + orderBySecond;
            }
        }
    }

    public static String getSelectSql(String sqlString, Object... args) {

        return getStaticSql(sqlString, args);
    }

    public static String getSqlWhere(String sqlString, Object... args) {

        return getStaticSql(sqlString, args);
    }


}
